//
//  Bucket.cpp
//  Demo
//
//  Created by CG on 13-10-9.
//
//

#include "Bucket.h"
#include <list>
using namespace std;


Bucket::Bucket()
:BaseObject::BaseObject()
{
    m_objectClassification = ObjectClassification_Bucket;
}

Bucket::~Bucket()
{

}


void Bucket::initWithFileAndType(const char *pszFilename, ObjectType objType)
{
    m_objectType = objType;
    
    CCSprite* bucket = CCSprite::create(pszFilename);
    this->addChild(bucket, this->getZOrder(), kBucketTag);
}



void Bucket::SetObjectPosition(const cocos2d::CCPoint &position)
{
    if (this->getChildByTag(kBucketTag))
    {
        this->getChildByTag(kBucketTag)->setPosition(position);
    }
}
void Bucket::SetObjectScale(const float scale)
{
    if (this->getChildByTag(kBucketTag))
    {
        this->getChildByTag(kBucketTag)->setScale(scale);
    }
}
void Bucket::SetObjectRotation(const float fRotation)
{
    if (this->getChildByTag(kBucketTag))
    {
        this->getChildByTag(kBucketTag)->setRotation(fRotation);
    }
}


void Bucket::CreateBody(b2World* pWorld)
{
    list<CCPoint> pointList;
    pointList.push_back(CCPointMake(320.0f / kPTM_RATIO, 50.0f / kPTM_RATIO));
    pointList.push_back(CCPointMake(320.0f / kPTM_RATIO, 30.0f / kPTM_RATIO));
    pointList.push_back(CCPointMake(360.0f / kPTM_RATIO, 30.0f / kPTM_RATIO));
    pointList.push_back(CCPointMake(360.0f / kPTM_RATIO, 50.0f / kPTM_RATIO));

    
    b2BodyDef bodyDef;
    bodyDef.position.Set(0 / kPTM_RATIO, 0 / kPTM_RATIO);
    
    m_pBucketBody = pWorld->CreateBody(&bodyDef);
    //////////////////
    list<CCPoint>::iterator preIter = pointList.begin();
    
    for(list<CCPoint>::iterator iter = pointList.begin();
        iter != pointList.end();
        ++ iter)
    {
        if (preIter != iter)
        {
            b2EdgeShape edgeShape;
            edgeShape.Set(b2Vec2(((CCPoint)*preIter).x, ((CCPoint)*preIter).y),
                          b2Vec2(((CCPoint)*iter).x, ((CCPoint)*iter).y));
            
            b2FixtureDef fixtureDef;
            fixtureDef.shape = &edgeShape;
            fixtureDef.density = 0.0f;
            fixtureDef.friction = 0.5f;
            fixtureDef.restitution = 0.5f;
            m_pBucketBody->CreateFixture(&fixtureDef);
            
            preIter = iter;
        }
    }
    
    
    b2BodyDef bucketBodyDef;
    bucketBodyDef.position.Set(0, 0);
    bucketBodyDef.userData = this;
    m_pBody = pWorld->CreateBody(&bucketBodyDef);
    
    b2EdgeShape edgeShape;
    edgeShape.Set(b2Vec2(322.0f / kPTM_RATIO, 32.0f / kPTM_RATIO),
                  b2Vec2(358.0f / kPTM_RATIO, 28.0f / kPTM_RATIO));
    
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &edgeShape;
    fixtureDef.density = 0.0f;
    fixtureDef.friction = 0.5f;
    fixtureDef.restitution = 0.5f;
    m_pBody->CreateFixture(&fixtureDef);
}